package com.Leetcode;

public class Index2048NextBeautifulNumber {

	public static void main(String[] args) {
		System.out.println(nextBeautifulNumber(6645));
	}
	
	public static int nextBeautifulNumber(int n) {
        if(n == 0) return 1;
        if(n < 22) return 22;
        if(n > 55555) return 122333;
        for(int i = n+1;i <= 55555;i++)
            if(isBealanceNum(i)) return i;
        return -1;
    }

    public static boolean isBealanceNum(int n){
        int[] a = new int[10];
        while(n > 0){
            a[n%10]++;
            n/=10;
        }
        for(int i = 0; i < a.length; i++){
            if(a[i] != i && a[i] != 0) return false;
        }
        return true;
    }
}
